<article id="wikiArticle">
<div></div>
<p> <strong><code>set()</code></strong> 方法用于从指定数组中读取值，并将其存储在类型化数组中。</p>
<h2 id="语法格式">语法格式</h2>
<pre><code class="language-javascript"><var>typedarr</var>.set(array [,offset])
typedarr.set(typedarray [,offset])
</code></pre>
<h3 id="参数">参数</h3>
<dl>
<dt>array</dt>
<dd>参数array是源数组，指定从哪里拷贝值。源数组中的所有值都会被拷贝到目标数组中去。如果源数组的长度加上偏移值offset的结果超过目标数组的长度，则会抛出异常错误。</dd>
<dt>typedarray</dt>
<dd>如果源数组是一个类型化数组（typed array），则源数组和目标数组会共享同一个底层的<a href="Reference/Global_Objects/ArrayBuffer" title="The source for this interactive example is stored in a GitHub repository. If you'd like to contribute to the interactive examples project, please clone https://github.com/mdn/interactive-examples and send us a pull request."><code>ArrayBuffer</code></a>；浏览器将会智能地将buffer的指定区段拷贝到目标区段中去。</dd>
<dt>offset <span class="inlineIndicator optional optionalInline">可选</span></dt>
<dd>偏移量参数offset指定从什么地方开始使用源数组<code>array</code>的值进行重写。如果忽略该参数，则默认为0（也就是说，从目标数组的下标为0处开始，使用源数组<code>array</code>的值覆盖重写）。</dd>
</dl>
<h3 id="返回值">返回值</h3>
<p><a href="Reference/Global_Objects/undefined" title="undefined是全局对象的一个属性。也就是说，它是全局作用域的一个变量。undefined的最初值就是原始数据类型undefined。"><code>undefined</code></a>.</p>
<h3 id="抛出的错误异常">抛出的错误异常</h3>
<dl>
<dt><a href="Reference/Global_Objects/RangeError" title="RangeError对象标明一个错误，当一个值不在其所允许的范围或者集合中。"><code>RangeError</code></a></dt>
<dd>如果指定的偏移量超出了类型化数组的范围，则该异常会被抛出。</dd>
</dl>
<h2 id="例子">例子</h2>
<h3 id="使用set方法"><code>使用set方法</code></h3>
<pre><code class="language-js">var buffer = new ArrayBuffer(8);
var uint8 = new Uint8Array(buffer);

uint8.set([1,2,3], 3);

console.log(uint8); // Uint8Array [ 0, 0, 0, 1, 2, 3, 0, 0 ]
</code></pre>
<h2 id="规范文档">规范文档</h2>
<table class="standard-table">
<tbody>
<tr>
<th scope="col">文档</th>
<th scope="col">状态</th>
<th scope="col">说明</th>
</tr>
<tr>
<td><a class="external" href="https://www.khronos.org/registry/typedarray/specs/latest/" hreflang="en" lang="en" rel="noopener" title="Typed Array Specification">Typed Array Specification</a></td>
<td><span class="spec-Obsolete">Obsolete</span></td>
<td>ECMAScript 6中已废弃。</td>
</tr>
<tr>
<td><a class="external" href="https://www.ecma-international.org/ecma-262/6.0/#sec-%typedarray%.prototype.set-array-offset" hreflang="en" lang="en" rel="noopener">ECMAScript 2015 (6th Edition, ECMA-262)<br/><small lang="zh-CN">TypedArray.prototype.set</small></a></td>
<td><span class="spec-Standard">Standard</span></td>
<td>在ECMA标准中初始定义。</td>
</tr>
<tr>
<td><a class="external" href="https://tc39.github.io/ecma262/#sec-%typedarray%.prototype.set-array-offset" hreflang="en" lang="en" rel="noopener">ECMAScript Latest Draft (ECMA-262)<br/><small lang="zh-CN">TypedArray.prototype.set</small></a></td>
<td><span class="spec-Draft">Draft</span></td>
<td> </td>
</tr>
</tbody>
</table>
<h2 id="浏览器兼容性">浏览器兼容性</h2>
<p></p><div class="blockIndicator warning"><strong><a class="external" href="https://github.com/mdn/browser-compat-data" rel="noopener">We're converting our compatibility data into a machine-readable JSON format</a></strong>.
            This compatibility table still uses the old format,
            because we haven't yet converted the data it contains.
            <strong><a class="new" href="/zh-CN/docs/MDN/Contribute/Structures/Compatibility_tables" rel="nofollow">Find out how you can help!</a></strong></div>
<div class="htab">
<a id="AutoCompatibilityTable" name="AutoCompatibilityTable"></a>
<ul>
<li class="selected"><a>Desktop</a></li>
<li><a>Mobile</a></li>
</ul>
</div><p></p>
<div id="compat-desktop">
<table class="compat-table">
<tbody>
<tr>
<th>浏览器</th>
<th>Chrome</th>
<th>Firefox (Gecko)</th>
<th>Internet Explorer</th>
<th>Opera</th>
<th>Safari</th>
</tr>
<tr>
<td>最低版本</td>
<td>7.0</td>
<td><a href="/en-US/Firefox/Releases/4" title="Released on 2011-03-22.">4.0</a> (2)</td>
<td>10</td>
<td>11.6</td>
<td>5.1</td>
</tr>
</tbody>
</table>
</div>
<div id="compat-mobile">
<table class="compat-table">
<tbody>
<tr>
<th>浏览器</th>
<th>Android</th>
<th>Chrome for Android</th>
<th>Firefox Mobile (Gecko)</th>
<th>IE Mobile</th>
<th>Opera Mobile</th>
<th>Safari Mobile</th>
</tr>
<tr>
<td>最低版本</td>
<td>4.0</td>
<td><span style="color: #888;" title="Please update this with the earliest version of support.">(Yes)</span></td>
<td>4.0 (2)</td>
<td>10</td>
<td>11.6</td>
<td>4.2</td>
</tr>
</tbody>
</table>
</div>
<h2 id="另可参考">另可参考</h2>
<ul>
<li><a href="/en-US/docs/Web/JavaScript/Typed_arrays">JavaScript typed arrays</a></li>
<li><a href="Reference/Global_Objects/TypedArray" title="一个TypedArray 对象描述一个底层的二进制数据缓存区的一个类似数组(array-like)视图。事实上，没有名为 TypedArray的全局对象，也没有一个名为的 TypedArray构造函数。相反，有许多不同的全局对象，下面会列出这些针对特定元素类型的类型化数组的构造函数。在下面的页面中，你会找到一些不管什么类型都公用的属性和方法。"><code>TypedArray</code></a></li>
<li><a href="Reference/Global_Objects/ArrayBuffer" title="The source for this interactive example is stored in a GitHub repository. If you'd like to contribute to the interactive examples project, please clone https://github.com/mdn/interactive-examples and send us a pull request."><code>ArrayBuffer</code></a></li>
</ul>
</article>